package com.xhtt.modules.app.controller.inspection;

import com.xhtt.common.exception.RRException;
import com.xhtt.common.exception.RRExceptionEnum;
import com.xhtt.common.utils.Constant;
import com.xhtt.common.utils.DownloadFile;
import com.xhtt.common.utils.R;
import com.xhtt.common.utils.ReadExcel;
import com.xhtt.modules.inspection.service.IInsPointService;
import com.xhtt.modules.inspection.vo.InsPointVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Map;

/**
 * <p>
 * 巡检管理-巡检点
 * </p>
 *
 * @author xj
 * @since 2019-01-18
 */
@RestController
@RequestMapping("app/ins-point")
@Api(value = "巡检-检点", tags = "巡检-检点")
public class InsPointController {

    @Autowired
    private IInsPointService insPointService;

    @GetMapping(value = "/list")
    @ApiOperation("巡检点列表(web)")
    public R list(@RequestParam Map<String, Object> params) {
        return R.ok().put(Constant.DATA, insPointService.queryPage(params));
    }

    @PostMapping(value = "/add")
    @ApiOperation("新增巡检点(web)")
    public R add(@RequestBody InsPointVO insPointVo) {
        if (insPointVo == null) {
            throw new RRException(RRExceptionEnum.PARAM_IS_EMPTY);
        }
        insPointService.add(insPointVo);
        return R.ok();
    }

    @PostMapping(value = "/delete")
    @ApiOperation("删除巡检点(web)")
    public R delete(@RequestBody Integer[] ids) {
        insPointService.delete(Arrays.asList(ids));
        return R.ok();
    }

    @PostMapping(value = "/update")
    @ApiOperation("修改巡检点(web)")
    public R update(@RequestBody InsPointVO insPointVo) {
        insPointService.update(insPointVo);
        return R.ok();
    }

    @GetMapping(value = "/get/{id}")
    @ApiOperation("获取巡检点(web)")
    public R get(@PathVariable("id") Integer id) {
        return R.ok().put(Constant.DATA, insPointService.detail(id));
    }

    @PostMapping("/upload")
    @ApiOperation("巡检管理-标签导入")
    public R upload(MultipartFile file) {

        Map<Long, Map<Integer, String>> params = ReadExcel.readExcel(file, 1);

        if (CollectionUtils.isEmpty(params)) {
            throw new RRException("文件内容不能为空");
        }
        insPointService.upload(params);
        return R.ok();
    }

    @GetMapping(value = "/mouldExport")
    @ApiOperation("检点模板下载")
    public void mouldExport(HttpServletResponse response) {
        String fileName = "检点信息导入模板.xls";
        String path = "templates/outPoint.xls";
        DownloadFile.exportFile(fileName, path, response);
    }

    @GetMapping(value = "/export")
    @ApiOperation("标签导出")
    public void export(HttpServletResponse response, @RequestParam Map<String, Object> params) {
        insPointService.exportExcel(response, params);
    }

}
